< previous page page_304 next page >

Page 304
e10a6a0a59f035e870d90cca05b85fce.gif
    ' Make the results visible.
    picCanvas.Refresh
    picCanvas.Picture = picCanvas.Image
End Sub

' Erase the center triangle from this one.
Private Sub SierpinskiErase(ByVal depth As Integer, _
    ByVal x1 As Single, ByVal y1 As Single, _
    ByVal x2 As Single, ByVal y2 As Single, _
    ByVal x3 As Single, ByVal y3 As Single)
Dim newy As Single
Dim newx1 As Single
Dim newx2 As Single
Dim newx3 As Single
Dim points(1 To 3) As POINTAPI

    ' Find the corners of the middle triangle.
    newy = (y1 + y2) / 2
    newx1 = (3 * x1 + x3) / 4
    newx2 = (x1 + x3) / 2
    newx3 = (x1 + 3 * x3) / 4

    ' Erase the middle triangle.
    points(1).X = newx1
    points(1).Y = newy
    points(2).X = newx3
    points(2).Y = newy
    points(3).X = newx2
    points(3).Y = y1
    Polygon picCanvas.hdc, points(1), 3

    ' Recursively erase other subtriangles.
    If depth > 0 Then
        SierpinskiErase depth - 1, _
            x1, y1, newx1, newy, newx2, y1
        SierpinskiErase depth - 1, _
            newx1, newy, newx2, y2, newx3, newy
        SierpinskiErase depth - 1, _
            newx2, y1, newx3, newy, x3, y1
    End If
End Sub
Example program SierpG uses this code to create Sierpinski gaskets. Start by drawing curves of small depth (four or five) until you know how long the program takes on your computer. Your monitor probably does not have enough resolution to show the difference between gaskets of higher depths, anyway.

 
< previous page page_304 next page >